﻿using ERPPublic;
using MediatR;
using Newtonsoft.Json;
using SqlSugar;
using WarehouseManagement.Application.Command.ShipApplyCommand;
using WarehouseManagement.Domain.DTO.ShipApply;
using WarehouseManagement.Infrstructre.Interface;
using static ERPPublic.ParseAPI;
namespace WarehouseManagement.Application.CommandHandler.ShipApplyCommandHandlers
{
    public class SelectShipCommandHandlers : IRequestHandler<SelectShipCommand, PageDTO<ShowShipDTO>>
    {


        private readonly IShipRequestRepository _shipRequest;



        public SelectShipCommandHandlers(IShipRequestRepository shipRequest)
        {
            _shipRequest = shipRequest;

        }
        public async Task<PageDTO<ShowShipDTO>> Handle(SelectShipCommand request, CancellationToken cancellationToken)
        {
            List<Client> client = await GetClient(1, 100);
            List<Vendor> vendor = await GetVendor(1, 100);
            var apply = (from a in await _shipRequest.GetAll() select a).ToList();

            var list = (from a in apply
                        where a.IsDel == false &&a.ShipState==1
                        select new ShowShipDTO
                        {
                            SPRTopic = a.SPRTopic,
                            ShippRequestAppId = a.ShippRequestAppId,
                            ClientId = a.ClientId,
                            VendorId = a.VendorId,
                            ReturnProductId = a.ReturnProductId,
                            SaleContractProductId = a.SaleContractProductId,
                            ShipType = a.ShipType,
                            SRAApplicant = a.SRAApplicant,
                            SRAConsigner = a.SRAConsigner,
                            SRADueDate = a.SRADueDate,
                            SRANumber = a.SRANumber,
                            Name = a.Name,
                        }).ToList();
            if (!string.IsNullOrEmpty(request.ShipNum))
            {
                list = list.Where(x => x.SRANumber.Contains(request.ShipNum)).ToList();
            }
            if (!string.IsNullOrEmpty(request.ShipTopic))
            {
                list = list.Where(x => x.SPRTopic.Contains(request.ShipTopic)).ToList();
            }
            if (!string.IsNullOrEmpty(request.AppLyName))
            {
                list = list.Where(x => x.SRAApplicant.Contains(request.AppLyName)).ToList();
            }

            int totalcount = list.Count();
            int pagecount = (int)Math.Ceiling(totalcount * 1.0 / request.pageSize);


            PageDTO<ShowShipDTO> page = new PageDTO<ShowShipDTO>()
            {
                TotalCount = totalcount,
                PageCount = pagecount,
                list = list.OrderByDescending(x => x.ShippRequestAppId).Skip((request.pageIndex - 1) * request.pageSize).ToList()
            };

            return page;
        }




        /// <summary>
        /// 显示产品信息
        /// </summary>
        /// <param name="PName"></param>
        /// <param name="PNumber"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetClient(int Pindex, int Psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MSIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTkiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5bCP576OIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.ZjkGj2IxuBzKmF5ZuSyWXQW8TkVqBGLUDkJ86RSweFI";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/vendor/api/Manpower/GetManpowerList?Pindex=" + Pindex + "&Psize="+ Psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Client>>(postReponse);
            return json.data.list;
        }



        /// <summary>
        /// 显示产品信息
        /// </summary>
        /// <param name="PName"></param>
        /// <param name="PNumber"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetVendor(int Pindex, int Psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MSIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTkiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5bCP576OIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.ZjkGj2IxuBzKmF5ZuSyWXQW8TkVqBGLUDkJ86RSweFI";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/cg/api/Vendor/ProductTypeSelectShow?PageIndex=" + Pindex + "&PageSize=" + Psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Vendor>>(postReponse);
            return json.data.list;
        }







        /// <summary>
        /// 客户信息
        /// </summary>
        public class Client 
        {
            /// <summary>
            ///客户id
            /// </summary>
            [SugarColumn(IsIdentity = true, IsPrimaryKey = true)] // 主键和自增
            public int ClientId { get; set; }
            /// <summary>
            /// 客户名称
            /// </summary>
            public string? ClientName { get; set; }
            /// <summary>
            /// 客户编号
            /// </summary>
            public string? ClientNumber { get; set; }
            /// <summary>
            /// 主要联系人姓名
            /// </summary>
            public string? CPrimaryContact { get; set; }
            /// <summary>
            /// 手机号码
            /// </summary>
            public string? CPhone { get; set; }
            /// <summary>
            /// 公司地址
            /// </summary>
            public string? CompanyAddress { get; set; }
        }

        /// <summary>
        /// 供应商信息
        /// </summary>
        public class Vendor 
        {
            /// <summary>
            /// 供应商id
            /// </summary>
            [SugarColumn(IsIdentity = true, IsPrimaryKey = true)] // 主键和自增
            public int VendorId { get; set; }
            /// <summary>
            /// 供应商编号
            /// </summary>
            public string? VNumber { get; set; }
            /// <summary>
            /// 供应商名称
            /// </summary>
            public string? VName { get; set; }
            /// <summary>
            /// 联系人
            /// </summary>
            public string? VContact { get; set; }
            /// <summary>
            /// 手机号码
            /// </summary>
            public string? VPhone { get; set; }
            /// <summary>
            /// 公司地址
            /// </summary>
            public string? VFirmAddress { get; set; }
        }
    }
}
